/*******************************************************************************
 * Copyright (c) 2002, 2003 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation. Boris Pruessmann -
 * Fixed call to IPerforceServer.reopen().
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.dnd;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.IChangelist;
import net.sourceforge.perforce.core.resources.IP4File;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.widgets.Shell;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class PendingChangelistDropAdapter extends ViewerDropAdapter {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the current target object. */
  private IChangelist currentTarget;

  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for PendingChangelistDropAdapter.
   * 
   * @param viewer the associated viewer.
   */
  public PendingChangelistDropAdapter(Viewer viewer) {
    super(viewer);
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public boolean performDrop(Object data) {
    IPerforceServer server = currentTarget.getServer();

    IP4File[] localFiles = (IP4File[]) data;
    try {
      for (int i = 0; i < localFiles.length; i++) {
        if (localFiles[i].getServer() == server) {
          localFiles[i].getServer().reopen(currentTarget.getId(), null,
              new IPath[] {localFiles[i].getLocation()}, null);
        }
      }
    } catch (final PerforceException e) {
      final Shell shell = PerforceUIPlugin.getActiveShell();
      shell.getDisplay().asyncExec(new Runnable() {
        public void run() {
          ErrorDialog.openError(shell, Policy.bind("simpleInternal"),
              "Failed to reopen one or more files in the selected changelist.", e.getStatus());
        }
      });
    } finally {
      IPerforceServer[] servers = new IPerforceServer[] {server};
      PerforceCorePlugin.getPlugin().broadcastChangelistStateChanges(servers);
    }

    return true;
  }

  @Override
  public boolean validateDrop(Object target, int operation, TransferData transferType) {
    if (!(target instanceof IChangelist)) {
      currentTarget = null;
      return false;
    } else {
      if (LocalFileTransfer.getInstance().isSupportedType(transferType)) {
        currentTarget = (IChangelist) target;
        return true;
      } else {
        return false;
      }
    }
  }
}
